package com.epfresh.views.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.epfresh.R;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.bean.DeliveryDateNew;

import java.util.ArrayList;
import java.util.List;

/*
* author zzq
* 区分精准达以及标准达逻辑
* */
public class DeliveryDateNewDialog extends Dialog implements View.OnClickListener {
    private View root,ll_select_type;
    private ListView lvDate,lvDetail;
    private Button rButton, lButton;
    private TextView tvNormal,tvSenior;
    private View ivClose;

    private BaseAdapter dateAdapter;
    private BaseAdapter timeAdapter;

    private List<DeliveryDateNew> normalDeliveryDate;
    private List<DeliveryDateNew> expressDeliveryDate;
    private List<DeliveryDateNew> nowDate = new ArrayList<>();
    private List<DeliveryDateNew.TimeListBean> nowTime = new ArrayList<>();

    boolean isNormalDelivery = true;
    private OnPositiveClickListener onPositiveClickListener;
    private Context mContext;
    DeliveryDateNew.TimeListBean nowTimeBean;
    DeliveryDateNew nowDateBean;

    public DeliveryDateNewDialog(Context context) {
        super(context);
        mContext = context;

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(true);
        root = getLayoutInflater().inflate(R.layout.dialog_delivery_time_new, null);
        ll_select_type = root.findViewById(R.id.ll_select_type);
        tvNormal = root.findViewById(R.id.tv_normal);
        tvSenior = root.findViewById(R.id.tv_senior);
        tvNormal.setOnClickListener(this);
        tvSenior.setOnClickListener(this);
        lvDate = root.findViewById(R.id.lv_date);
        lvDetail = root.findViewById(R.id.lv_detail);
        rButton = root.findViewById(R.id.positiveButton);
        rButton.setOnClickListener(this);
        lButton = root.findViewById(R.id.negativeButton);
        lButton.setOnClickListener(this);
        ivClose = root.findViewById(R.id.iv_close);
        ivClose.setOnClickListener(this);
        setTitle("要求送达时间");
        setContentView(root);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = LocalDisplay.SCREEN_WIDTH_PIXELS;
        dialogWindow.setAttributes(lp);
        dialogWindow.setGravity(Gravity.BOTTOM);//底部出现

        dateAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                    return nowDate.size();
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder vh;
                if (convertView == null) {
                    convertView = getLayoutInflater().inflate(R.layout.item_list_market, null);
                    vh = new ViewHolder();
                    vh.tv = convertView.findViewById(R.id.tv_content);
                    vh.tv.setGravity(Gravity.CENTER);
                    convertView.setTag(vh);
                } else {
                    vh = (ViewHolder) convertView.getTag();
                }

                DeliveryDateNew market = nowDate.get(position);
                vh.tv.setText(market.getDateStr());
                if (market == nowDateBean){
                    vh.tv.setTextColor(Color.parseColor("#24AF75"));
                    vh.tv.setBackgroundColor(Color.WHITE);
                }else {
                    vh.tv.setTextColor(Color.parseColor("#434343"));
                    vh.tv.setBackgroundColor(Color.parseColor("#fdfdfd"));
                }

                return convertView;
            }
        };

        lvDate.setAdapter(dateAdapter);

        lvDate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                nowDateBean = nowDate.get(position);
                dateAdapter.notifyDataSetChanged();
                nowTime.clear();
                nowTime.addAll(nowDate.get(position).getTimeList());
                timeAdapter.notifyDataSetChanged();
            }
        });
        timeAdapter = new BaseAdapter() {

            @Override
            public int getCount() {
                return nowTime.size();
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder vh = null;
                if (convertView == null) {
                    convertView = getLayoutInflater().inflate(R.layout.item_list_data_time, null);
                    vh = new ViewHolder();
                    vh.tv = (TextView) convertView.findViewById(R.id.tv_content);
                    convertView.setTag(vh);
                } else {
                    vh = (ViewHolder) convertView.getTag();
                }

                DeliveryDateNew.TimeListBean timeListBean = nowTime.get(position);

                vh.tv.setText(timeListBean.getTitle()+" (运费"+timeListBean.getFreight()+"元)");

                if (timeListBean == nowTimeBean){
                    vh.tv.setTextColor(Color.parseColor("#24AF75"));
                    vh.tv.setBackgroundResource(R.mipmap.bd_select_time);
                }else {
                    vh.tv.setTextColor(Color.parseColor("#434343"));
                    vh.tv.setBackgroundColor(Color.WHITE);
                }
                return convertView;
            }
        };

        lvDetail.setAdapter(timeAdapter);

        lvDetail.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onPositiveClickListener != null) {

                    nowTime.get(position).setFather(nowDateBean);
                    nowTime.get(position).setTimePosition(position);
                    nowTime.get(position).setDatePosition(nowDate.indexOf(nowDateBean));
                    nowTime.get(position).setNormalDelivery(isNormalDelivery);
                    onPositiveClickListener.onPositiveClickListener(null, nowTime.get(position));
                }
                if (isPositiveDismiss)
                    dismiss();
            }
        });
    }

    public void setDates(List<DeliveryDateNew> normalDeliveryDate, List<DeliveryDateNew> expressDeliveryDate, DeliveryDateNew.TimeListBean nowBean) {
        if (normalDeliveryDate == null && expressDeliveryDate ==null){
            return;
        }
        this.normalDeliveryDate = normalDeliveryDate;
        this.expressDeliveryDate = expressDeliveryDate;
        this.nowTimeBean = nowBean;
        this.nowDateBean = nowBean.getFather();
        if(normalDeliveryDate!=null&&normalDeliveryDate.size()>0&&expressDeliveryDate!=null&&expressDeliveryDate.size()>0){
            ll_select_type.setVisibility(View.VISIBLE);
            if(nowBean.isNormalDelivery()){
                isNormalDelivery = true;
                tvSenior.setTextColor(Color.parseColor("#313131"));
                tvSenior.setBackgroundColor(Color.TRANSPARENT);
                tvNormal.setTextColor(Color.WHITE);
                tvNormal.setBackgroundResource(R.drawable.bd_select_time_type);
            }else{
                isNormalDelivery = false;
                tvSenior.setTextColor(Color.WHITE);
                tvSenior.setBackgroundResource(R.drawable.bd_select_time_type);
                tvNormal.setTextColor(Color.parseColor("#313131"));
                tvNormal.setBackgroundColor(Color.TRANSPARENT);
            }
        }else {
            ll_select_type.setVisibility(View.GONE);
        }
        nowDate.clear();
        if(nowBean.isNormalDelivery()){
            nowDate.addAll(normalDeliveryDate);
        }else{
            nowDate.addAll(expressDeliveryDate);
        }
        nowTime.clear();
        nowTime.addAll(nowBean.getFather().getTimeList());
        if (dateAdapter != null) {
            dateAdapter.notifyDataSetChanged();
        }
        if (timeAdapter != null) {
            timeAdapter.notifyDataSetChanged();
        }

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.positiveButton:

                if (isPositiveDismiss)
                    dismiss();
                if (onPositiveClickListener != null) {
                }
                break;
            case R.id.negativeButton:
                dismiss();
                break;
            case R.id.tv_normal:
                isNormalDelivery = true;
                tvSenior.setTextColor(Color.parseColor("#313131"));
                tvSenior.setBackgroundColor(Color.TRANSPARENT);
                tvNormal.setTextColor(Color.WHITE);
                tvNormal.setBackgroundResource(R.drawable.bd_select_time_type);
                nowDate.clear();
                nowDate.addAll(normalDeliveryDate);
                dateAdapter.notifyDataSetChanged();
                nowTime.clear();
                if(nowTimeBean.isNormalDelivery()){
                    nowDateBean = nowTimeBean.getFather();
                    nowTime.addAll(nowTimeBean.getFather().getTimeList());
                }else{
                    nowDateBean = normalDeliveryDate.get(0);
                    nowTime.addAll(nowDate.get(0).getTimeList());
                }
                timeAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_senior:
                isNormalDelivery = false;
                tvSenior.setTextColor(Color.WHITE);
                tvSenior.setBackgroundResource(R.drawable.bd_select_time_type);
                tvNormal.setTextColor(Color.parseColor("#313131"));
                tvNormal.setBackgroundColor(Color.TRANSPARENT);
                nowDate.clear();
                nowDate.addAll(expressDeliveryDate);
                dateAdapter.notifyDataSetChanged();
                nowTime.clear();
                if(!nowTimeBean.isNormalDelivery()){
                    nowDateBean = nowTimeBean.getFather();
                    nowTime.addAll(nowTimeBean.getFather().getTimeList());
                }else{
                    nowDateBean = expressDeliveryDate.get(0);
                    nowTime.addAll(nowDate.get(0).getTimeList());
                }
                timeAdapter.notifyDataSetChanged();


                break;
            case R.id.iv_close:
                this.dismiss();
                break;
        }
    }


    public OnPositiveClickListener getOnPositiveClickListener() {
        return onPositiveClickListener;
    }

    public void setOnPositiveClickListener(OnPositiveClickListener onPositiveClickListener) {
        this.onPositiveClickListener = onPositiveClickListener;
    }

    public void setTitle(String title) {
        ((TextView) root.findViewById(R.id.title)).setText(title);
    }

    public void setLeftText(String text) {
        ((TextView) root.findViewById(R.id.negativeButton)).setText(text);
    }

    public void setRightText(String text) {
        ((TextView) root.findViewById(R.id.positiveButton)).setText(text);
    }

    public boolean isPositiveDismiss() {
        return isPositiveDismiss;
    }

    public void setPositiveDismiss(boolean positiveDismiss) {
        isPositiveDismiss = positiveDismiss;
    }

    private boolean isPositiveDismiss;

    public interface OnPositiveClickListener {
        public void onPositiveClickListener(Object o, DeliveryDateNew.TimeListBean market);
    }
    class ViewHolder {
        TextView tv;
    }
}